<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "set_user_up".
 *
 * @property int $id
 * @property float $r1_1 直推1
 * @property float $r1_2 直推2
 * @property float $r2_1 间推1
 * @property float $r2_2 间推2
 * @property float $total 购买多少有推荐码
 */
class SetUserUp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_user_up';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['r1_1', 'r1_2', 'r2_1', 'r2_2', 'total'], 'number'],

            [['address', 'tel', 'name'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'r1_1' => 'R 1  1',
            'r1_2' => 'R 1  2',
            'r2_1' => 'R 2  1',
            'r2_2' => 'R 2  2',
            'total' => 'Total',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetUserUp' => $data]) && $model->save()) {
            return true;
        }
    }
}
